package com.alibaba.dubbo.demo.provider.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.demo.SysDeptInfoInterface;
import com.alibaba.dubbo.demo.bean.PageBean;
import com.alibaba.dubbo.demo.bean.SysDeptInfo;

public class SysDeptInfoDaoImpl  extends BaseDAO implements SysDeptInfoInterface {
	Logger logger = Logger.getLogger(SysDeptInfoDaoImpl.class);
	@Override
	public PageBean queryAllDeptInfo(PageBean pageBean,boolean all) {
		// TODO Auto-generated method stub
		String msql = "from SysDeptInfo";
		String countSql = "select count(*) " + msql;
		return getPageBean(msql, countSql, pageBean);
	}

	@Override
	public List<SysDeptInfo> queryAllDeptInfoList(boolean all) {
		// TODO Auto-generated method stub
		String msql = "from SysDeptInfo";
		List<SysDeptInfo> datas = queryDeptTree(all,0,"");
		return datas;
	}
	
	@Override
	public List<SysDeptInfo> queryDeptTree(boolean all,int deptid,String deptName) {
		// TODO Auto-generated method stub
		String msql = "from SysDeptInfo where upperid=0";
		if(deptid!=0)
			msql = "from SysDeptInfo where deptid="+Integer.valueOf(deptid).toString();
		else 
		{
			System.out.println("queryDeptTree deptName="+deptName);
			if(!StringUtils.isEmpty(deptName))
			{
				msql = "from SysDeptInfo where deptname like '%"+deptName+"%'";
			}
		}
		
		if(!all)
			msql += " and isvalid=1";
		
		List<SysDeptInfo> rootdeptList = queryBean(msql);
		List<SysDeptInfo> datas = new ArrayList<SysDeptInfo>();
		for(SysDeptInfo item : rootdeptList){
			datas.add(item);
			//if(item.getChildnum()>0)
			{
				GetChildDept(datas,all,item.getDeptid());
			}
		}
		return datas;
	}
	
	public void GetChildDept(List<SysDeptInfo> datas,boolean all,int upperid)
	{
		List<SysDeptInfo> deptList = queryDeptInfoListByUpperDept(all,upperid);
		if(deptList==null)
			return ;
		for(SysDeptInfo item : deptList){
			datas.add(item);
			//if(item.getChildnum()>0)
			{
				GetChildDept(datas,all,item.getDeptid());
			}				
		}
	}
	
	@Override
	public PageBean queryDeptInfoByUpperDept(boolean all,int deptid) {
		// TODO Auto-generated method stub
		/*String msql = "from SysDeptInfo where upperid="+Integer.valueOf(deptid).toString();
		if(!all)
			msql +=" and isvalid=1";
		//String countSql = "select count(*) " + msql;
		System.out.println("queryDeptInfoByUpperDept  msql="+msql); */
		return new PageBean("0", "20");
	}

	@Override
	public List<SysDeptInfo> queryDeptInfoListByUpperDept(boolean all,int deptid) {
		// TODO Auto-generated method stub
		String msql = "from SysDeptInfo where upperid="+deptid;
		if(!all)                              
			msql += " and isvalid=1";
		List<SysDeptInfo> datas = queryBean(msql);
		return datas;
	}

	@Override
	public PageBean queryDeptInfoByDept(int deptid) {
		// TODO Auto-generated method stub
		String msql = "from SysDeptInfo where =deptid"+deptid;
		String countSql = "select count(*) " + msql;
		return new PageBean(msql, countSql);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SysDeptInfo> queryDeptInfoListByDept(int deptid) {
		// TODO Auto-generated method stub
		String msql = "from SysDeptInfo where deptid="+deptid;
		List<SysDeptInfo> datas = queryBean(msql);
		return datas;
	}

	@Override
	public boolean saveDeptInfo(SysDeptInfo deptInfo) {
		// TODO Auto-generated method stub
		try {
			getHibernateTemplate().save(deptInfo);
			updateDeptChildNum(deptInfo.getUpperid(),1);
			return true;
		} catch (Exception e) {
			logger.error("新增通知异常！", e);
			return false;
		}
	}

	@Override
	public boolean updateDeptInfo(SysDeptInfo deptInfo) {
		// TODO Auto-generated method stub
		try {
			getHibernateTemplate().update(deptInfo);
			return true;
		} catch (Exception e) {
			logger.error("更新通知异常！", e);
			return false;
		}
	}

	@Override
	public boolean deleteDeptInfo(SysDeptInfo deptInfo) {
		// TODO Auto-generated method stub
		try {
			getHibernateTemplate().delete(deptInfo);
			updateDeptChildNum(deptInfo.getUpperid(),0);
			return true;
		} catch (Exception e) {
			logger.error("删除通知异常！", e);
			return false;
		}
	}

	@Override
	public SysDeptInfo queryOneDeptInfoByDept(int deptid) {
		// TODO Auto-generated method stub
		List<SysDeptInfo> list = queryDeptInfoListByDept(deptid);
		if(list==null)
			return new SysDeptInfo();
		else
		{
			if(list.size()>0)
				return list.get(0);
			else 
				return  new SysDeptInfo();
		}
	}
	//type 1:增加  0:减少
	public boolean updateDeptChildNum(int deptid,int type)
	{
		String sql = "update sys_deptinfo t set t.childnum = t.childnum + 1 where t.deptid=1";
		if(type==0)		
			sql = "update sys_deptinfo t set t.childnum = t.childnum - 1 where t.deptid=1";
		return excuteBySql(sql)>0;
	}
}
